﻿Imports System.IO
Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports GeoFramework
Imports GeoFramework.Gps
Imports GeoFramework.Gps.IO
Imports GeoFramework.Gps.Nmea


Public Class DeviceTestOutils
    ' recuperer le nom du device (objet device de GeoFramework)  utilisé par ePocket pour lire les données GPS 
    ' Dim strportName As String
    ' Dim oMonDevice As GeoFramework.Gps.IO.SerialDevice
    'Dim strNomDevice As String
    Public oCurrentDevice As Device

    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Show()
        Application.DoEvents()


        ' AddHandler Devices.DeviceDetectionStarted, AddressOf Devices_DeviceDetectionStarted
        ' AddHandler Devices.DeviceDetected, AddressOf Devices_DeviceDetected
        ' AddHandler Devices.DeviceDetectionCompleted, AddressOf Devices_DeviceDetectionCompleted
        ' AddHandler Devices.DeviceDetectionAttempted, AddressOf Devices_DeviceDetectionAttempted
        ' AddHandler Devices.DeviceDetectionAttemptFailed, AddressOf Devices_DeviceDetectionAttemptFailed

       

        'AddHandler NmeaInterpreter1.SpeedChanged, AddressOf NmeaInterpreter1_SpeedChanged
        AddHandler NmeaInterpreter1.FixAcquired, AddressOf NmeaInterpreter1_FixAcquired
        AddHandler NmeaInterpreter1.FixLost, AddressOf NmeaInterpreter1_FixLost
        AddHandler NmeaInterpreter1.Resumed, AddressOf NmeaInterpreter1_Resumed
        AddHandler NmeaInterpreter1.FixModeChanged, AddressOf NmeaInterpreter1_FixModeChanged
        AddHandler NmeaInterpreter1.FixQualityChanged, AddressOf NmeaInterpreter1_FixQualityChanged
        AddHandler NmeaInterpreter1.HorizontalDilutionOfPrecisionChanged, AddressOf NmeaInterpreter1_HorizontalDilutionOfPrecisionChanged
        'AddHandler NmeaInterpreter1.SentenceReceived, AddressOf NmeaInterpreter1_SentenceReceived
        AddHandler NmeaInterpreter1.AltitudeChanged, AddressOf NmeaInterpreter1_AltitudeChanged
        AddHandler NmeaInterpreter1.PositionChanged, AddressOf NmeaInterpreter1_PositionChanged
        AddHandler NmeaInterpreter1.UtcDateTimeChanged, AddressOf NmeaInterpreter1_UtcDateTimeChanged
        AddHandler NmeaInterpreter1.Paused, AddressOf NmeaInterpreter1_Paused
        AddHandler NmeaInterpreter1.BearingChanged, AddressOf NmeaInterpreter1_BearingChanged
        AddHandler NmeaInterpreter1.VerticalDilutionOfPrecisionChanged, AddressOf NmeaInterpreter1_VerticalDilutionOfPrecisionChanged
        AddHandler NmeaInterpreter1.SatellitesChanged, AddressOf NmeaInterpreter1_SatellitesChanged
        'Timer
        ' Timer1.Enabled = True
        'Timer1.Interval = 500
        'AddHandler Timer1.Tick, AddressOf Timer1_Tick

        'compass1.IsPaintingOnSeparateThread = false
        'Altimeter1.IsPaintingOnSeparateThread = False
        'speedometer1.IsPaintingOnSeparateThread = false
        'satelliteViewer1.IsPaintingOnSeparateThread = false
        'satelliteSignalBar1.IsPaintingOnSeparateThread = false
        'strportName = frmParamGPS.LST_Ports.SelectedItem.ToString

        ' GeoFramework.Gps.Diagnostics.Run(New StreamWriter("GPS Diagnostics Log.txt"))
        NmeaInterpreter1.Start(oCurrentDevice)

    End Sub
#Region "GPS Device : Detection des évènements"

    'Private Sub Devices_DeviceDetectionStarted(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '     BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionStarted), sender, e)
    ' End Sub
    ' Private Sub DeviceDetectionStarted(ByVal sender As Object, ByVal e As EventArgs)
    '   StatusBar1.Text = "Detection des appareils GPS ..."
    ' SearchButton.Enabled = False
    'End Sub


    ' Private Sub Devices_DeviceDetected(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '    BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetected), sender, e)
    ' End Sub
    ' Private Sub DeviceDetected(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '    StatusBar1.Text = "Appareil trouvé: " + e.Device.ToString()
    '  Dim DeviceItem As New ListViewItem(e.Device.ToString())
    ' Associate the detected device with this list view item, so we can connect to the device later on
    '  DeviceItem.Tag = e.Device
    'Set the image of the new item
    '    DeviceItem.ImageIndex = 0
    '    DevicesListView.Items.Add(DeviceItem)
    'End Sub


    'Private Sub Devices_DeviceDetectionCompleted(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '     BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionCompleted), sender, e)
    ' End Sub
    ' Private Sub DeviceDetectionCompleted(ByVal sender As Object, ByVal e As EventArgs)
    ' StatusBar1.Text = "detection completée"
    ' SearchButton.Enabled = True
    ' StartButton.Enabled = True
    'End Sub


    'Private Sub Devices_DeviceDetectionAttempted(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '   BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionAttempted), sender, e)
    ' End Sub
    ' Private Sub DeviceDetectionAttempted(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceEventArgs)
    '   StatusBar1.Text = "Detection  " + e.Device.ToString()
    'End Sub

    'Private Sub Devices_DeviceDetectionAttemptFailed(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceDetectionExceptionEventArgs)
    '    BeginInvoke(New Action(Of Object, EventArgs)(AddressOf DeviceDetectionAttemptFailed), sender, e)
    ' End Sub
    ' Private Sub DeviceDetectionAttemptFailed(ByVal sender As Object, ByVal e As GeoFramework.Gps.IO.DeviceDetectionExceptionEventArgs)
    '    StatusBar1.Text = "Erreur: " + e.Exception.ToString()
    ' End Sub

    ' Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'GeoFramework.Gps.IO.Devices.BeginDetection()


    ' End Sub


#End Region


#Region "interpreteur NMEA  : Detection des évènements"
    Private Sub NmeaInterpreter1_AltitudeChanged(ByVal sender As Object, ByVal e As GeoFramework.DistanceEventArgs) Handles NmeaInterpreter1.AltitudeChanged
        BeginInvoke(New Action(Of Object, GeoFramework.DistanceEventArgs)(AddressOf AltitudeChanged), sender, e)

    End Sub

    Private Sub AltitudeChanged(ByVal sender As Object, ByVal e As GeoFramework.DistanceEventArgs)
        'Altimeter1.Value = e.Distance
        Altitude.Text = e.Distance.ToLocalUnitType().ToString()
    End Sub


    ' Private Sub NmeaInterpreter1_SpeedChanged(ByVal sender As System.Object, ByVal e As GeoFramework.SpeedEventArgs) Handles NmeaInterpreter1.SpeedChanged

    ' End Sub

    Private Sub NmeaInterpreter1_FixAcquired(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NmeaInterpreter1.FixAcquired

    End Sub

    Private Sub NmeaInterpreter1_FixLost(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NmeaInterpreter1.FixLost

    End Sub

    Private Sub NmeaInterpreter1_Resumed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NmeaInterpreter1.Resumed

    End Sub

    Private Sub NmeaInterpreter1_FixModeChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.FixModeEventArgs) Handles NmeaInterpreter1.FixModeChanged

    End Sub

    Private Sub NmeaInterpreter1_FixQualityChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.FixQualityEventArgs) Handles NmeaInterpreter1.FixQualityChanged

    End Sub

    Private Sub NmeaInterpreter1_HorizontalDilutionOfPrecisionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs) Handles NmeaInterpreter1.HorizontalDilutionOfPrecisionChanged

    End Sub

    'Private Sub NmeaInterpreter1_SentenceReceived(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs) Handles NmeaInterpreter1.SentenceReceived

    'End Sub

    Private Sub NmeaInterpreter1_PositionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.PositionEventArgs) Handles NmeaInterpreter1.PositionChanged

    End Sub

    Private Sub NmeaInterpreter1_UtcDateTimeChanged(ByVal sender As System.Object, ByVal e As GeoFramework.DateTimeEventArgs) Handles NmeaInterpreter1.UtcDateTimeChanged

    End Sub

    Private Sub NmeaInterpreter1_Paused(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NmeaInterpreter1.Paused

    End Sub

    Private Sub NmeaInterpreter1_BearingChanged(ByVal sender As System.Object, ByVal e As GeoFramework.AzimuthEventArgs) Handles NmeaInterpreter1.BearingChanged

    End Sub

    Private Sub NmeaInterpreter1_VerticalDilutionOfPrecisionChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.DilutionOfPrecisionEventArgs) Handles NmeaInterpreter1.VerticalDilutionOfPrecisionChanged

    End Sub
    Private Sub NmeaInterpreter1_SatellitesChanged(ByVal sender As System.Object, ByVal e As GeoFramework.Gps.SatelliteListEventArgs) Handles NmeaInterpreter1.SatellitesChanged

    End Sub
#End Region

#Region "Connection et Deconnection"
    '  Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' If DevicesListView.SelectedIndices.Count = 0 Then

    '   MessageBox.Show("Merci de sélectionner un appareil pour commencer")

    '   Return

    ' End If

    ' Find out which item is selected in the list view

    ' Dim SelectedItem As ListViewItem = DevicesListView.Items(DevicesListView.SelectedIndices(0))



    ' The "tag" property holds the device that was detected

    ' Dim selectedDevice As Device = DirectCast(SelectedItem.Tag, Device)



    '   Try
    '    NmeaInterpreter1.Start(oCurrentDevice)
    '   AddHandler selectedDevice.Connecting, AddressOf Device_Connecting

    '   AddHandler selectedDevice.Connected, AddressOf Device_Connected

    '   AddHandler selectedDevice.Disconnecting, AddressOf Device_Disconnecting

    '  AddHandler selectedDevice.Disconnected, AddressOf Device_Disconnected



    '   StartButton.Enabled = False

    ' Enable the disconnect button

    '   StopButton.Enabled = True

    '  Catch ex As Exception
    '     MessageBox.Show(ex.ToString(), "Connexion impossible", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    ' End Try

    ' Finally, start the interpreter using the newly-assigned Stream

    '   End Sub
    '  Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' arreter toute communication
    '   NmeaInterpreter1.Stop()

    '   StartButton.Enabled = True
    ' désactiver le bouton deconnexion
    '   StopButton.Enabled = False
    '   StatusBar1.Text = "déconnexion du GPS"
    'End Sub

    '  Private Sub DevicesListView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    '   StartButton.Enabled = True
    '   StartButton.Focus()
    ' End Sub

    ' Sub Device_Connecting(ByVal sender As Object, ByVal e As EventArgs)

    '   Cursor.Current = Cursors.WaitCursor

    '   StatusBar1.Text = "Connection à l'appareil GPS..."

    '  End Sub



    '  Sub Device_Connected(ByVal sender As Object, ByVal e As EventArgs)

    '   StatusBar1.Text = "Connecté au GPS!  lecture de données..."

    '   Cursor.Current = Cursors.[Default]

    ' End Sub

    '   Sub Device_Disconnecting(ByVal sender As Object, ByVal e As System.EventArgs)

    '    Try

    '     StatusBar1.Text = "Disconnexion du GPS ..."

    '     Cursor.Current = Cursors.WaitCursor

    '  Catch

    ' End Try

    ' End Sub



    '  Sub Device_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs)

    '   Try

    '   StatusBar1.Text = "Deconnecté."

    '   Cursor.Current = Cursors.[Default]

    ' Catch

    '   End Try

    '  End Sub




#End Region






    'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    'End Sub


    'Private Sub DeviceTestOutils_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '  LblDevice.Text = strNomDevice
    '  If oMonDevice.IsOpen Then
    '      LblTest.Text = "oui"
    '   Else
    '        LblTest.Text = "non"
    '   End If
    '  End Sub

    ' Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    oMonDevice = New SerialDevice(strportName)
    '    strNomDevice = oMonDevice.Name

    '   NmeaInterpreter1.Start(oMonDevice)

    ' End Sub

    ' Private Sub DevicesListView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    '  End Sub






End Class